package com.stottlerhenke.simbionic.editor.parser;

import com.stottlerhenke.simbionic.api.SB_VarType;
import com.stottlerhenke.simbionic.common.parser.EBinaryOp;


public class SB_BinaryOpNode extends SB_ParseNode {
  private SB_ParseNode _arg1;
  private SB_ParseNode _arg2;
  private EBinaryOp _op;

  public SB_BinaryOpNode(SB_ParseNode arg1, SB_ParseNode arg2, EBinaryOp op, SB_VarType type) {
    _arg1 = arg1;
    _arg2 = arg2;
    _op = op;
    _type = type; //DetermineResultType();
  }
  public SB_ErrorNode GetNextError(SB_ErrorNode lastError) {
    SB_ErrorNode nextError = _arg1.GetNextError(lastError);
    if (nextError == null)
    {
      nextError = _arg2.GetNextError(lastError);
    }
    return nextError;
  }
  public boolean IsValid(){
    return (_arg1.IsValid() && _arg2.IsValid());
  }
  public boolean ContainsVariable(String varName) {
    return (_arg1.ContainsVariable(varName) || _arg2.ContainsVariable(varName));
  }
  public boolean ContainsFunction(String funcName) {
    return (_arg1.ContainsFunction(funcName) || _arg2.ContainsFunction(funcName));
  }
  public String ToString() {
    return new String(_arg1.ToString() + OpToString(_op) + _arg2.ToString());
  }
  
  
  public static String OpToString(EBinaryOp op){
    if(op == EBinaryOp.kAddOp)
      return new String("+");
    else if(op == EBinaryOp.kSubOp)
      return new String("-");
    else if(op == EBinaryOp.kMultOp)
      return new String("*");
    else if(op == EBinaryOp.kDivOp)
      return new String("/");
    else if(op == EBinaryOp.kModOp)
      return new String("%");
    else if(op == EBinaryOp.kExpOp)
      return new String("^");
    else if(op == EBinaryOp.kLssOp)
      return new String("<");
    else if(op == EBinaryOp.kLeqOp)
      return new String("<=");
    else if(op == EBinaryOp.kEqlOp)
      return new String("==");
    else if(op == EBinaryOp.kNeqOp)
      return new String("!=");
    else if(op == EBinaryOp.kGrtOp)
      return new String(">");
    else if(op == EBinaryOp.kGeqOp)
      return new String(">=");
    else if(op == EBinaryOp.kAndOp)
      return new String("&&");
    else if(op == EBinaryOp.kOrOp)
      return new String("||");
    else if(op == EBinaryOp.kDotOp)
      return new String(".");
    else return new String("UNKNOWN BINARY-OP");

   }
   /**
    * @return the binary operator
    */
   public EBinaryOp GetOperator() { return _op; }

   /**
    * @return the left-hand argument
    */
   public SB_ParseNode GetLeftArg() { return _arg1; }

   /**
    * @return the right-hand argument
    */
   public SB_ParseNode GetRightArg() { return _arg2; }


}